home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / mail / mailleds.93 / mailleds / mailleds-0.93 / pid.c < prev    next >
C/C++ Source or Header  |  1996-05-14  |  1KB  |  64 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <signal.h>
  5. #include "config.h"
  6. #include "mailleds.h"
  7.  
  8. extern int uid;
  9. extern int euid;
  10. extern int pid;
  11. extern char opt_q;
  12. extern char *pid_filename;
  13. extern char *username;
  14.  
  15. void set_pidfilename(void)
  16. {
  17.     int size = (strlen(PIDFILE_DIR) + strlen("/mailleds-") + strlen(username) + strlen(".pid") + 1);
  18.     pid_filename = (char *) xmalloc(size);
  19.     sprintf(pid_filename, "%s/mailleds-%s.pid", PIDFILE_DIR, username);
  20. }
  21.  
  22. void write_pidfile(void)
  23. {
  24.     FILE *pidfile;
  25.     if ((pidfile = fopen(pid_filename, "w")) == (FILE *) NULL) {
  26.         fprintf(stderr, "mailleds: could not write pidfile %s\n", pid_filename);
  27.         exit(-2);
  28.     }
  29.     fprintf(pidfile, "%d", pid);
  30.     fclose(pidfile);
  31. }
  32.  
  33. int get_pid_from_file(char *pid_filename)
  34. {
  35.     char buf[1024];
  36.     char *inc;
  37.     FILE *pid_file;
  38.     if ((pid_file = fopen(pid_filename, "r"))) {
  39.         fgets(buf, 1024, pid_file);
  40.         fclose(pid_file);
  41.         inc = buf;
  42.         while (isspace(*inc))
  43.             inc++;
  44.         return (atoi(inc));
  45.     } else {
  46.         return (0);
  47.     }
  48. }
  49.  
  50. int pid_check(void)
  51. {
  52.     int test_pid;
  53.     test_pid = get_pid_from_file(pid_filename);
  54.     if (test_pid) {
  55.         if (kill(test_pid, 0) < 0 && errno == ESRCH)
  56.             return (1);    /* pidfile is stale -- override. */
  57.         if (!opt_q)
  58.             fprintf(stderr, "mailleds: mailleds process %d already running for %s\n", test_pid, username);
  59.         exit(-2);
  60.     } else {
  61.         return (1);
  62.     }
  63. }
  64.